<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ShopPostageRegionModel extends AbstractModel
{
    protected $tableName = 't_shop_postage_region';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws Exception|Throwable
     */
    public function getPostage($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addPostage($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('数据-快递-区域');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colBigInt('pid', 20)->setIsNotNull()->setColumnComment('上级PID');
        $table->colVarChar('first', 50)->setDefaultValue('')->setColumnComment('首字母');
        $table->colVarChar('short', 100)->setDefaultValue('')->setColumnComment('区域简称');
        $table->colVarChar('name', 100)->setDefaultValue('')->setColumnComment('区域名称');
        $table->colTinyInt('level',4)->setDefaultValue(0)->setColumnComment('区域层级');
        $table->colVarChar('code', 100)->setDefaultValue('')->setColumnComment('区域邮编');
        $table->colVarChar('pinyin', 100)->setDefaultValue('')->setColumnComment('区域拼音');
        $table->colVarChar('lng', 100)->setDefaultValue('')->setColumnComment('lng');
        $table->colVarChar('lat', 100)->setDefaultValue('')->setColumnComment('lat');
        $table->colTinyInt('status',1)->setDefaultValue(1)->setColumnComment('商品状态(1使用,0禁用)');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('idx_base_postage_region_pid', 'pid');
        $table->indexNormal('idx_base_postage_region_name', 'name');
        return $table;
    }
}